package com.springboot.astra.controller;

import com.springboot.astra.bean.Permission;
import com.springboot.astra.bean.Resource;
import com.springboot.astra.bean.RoleResource;
import com.springboot.astra.bean.RolesPermission;
import com.springboot.astra.result.DataResult;
import com.springboot.astra.service.PermissionService;
import com.springboot.astra.service.ResourceService;
import com.springboot.astra.service.RoleResourceService;
import com.springboot.astra.service.RolesPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    PermissionService permissionService;
    @Autowired
    RolesPermissionService rolesPermissionService;
    @Autowired
    RoleResourceService roleResourceService;
    @Autowired
    ResourceService resourceService;


    @GetMapping("/getAll")
    public DataResult getAll() {
        List<Permission> permissions = permissionService.getAll();
        return DataResult.success(permissions);
    }
    @PutMapping("/insertPermissionForRole")
    public DataResult insertPermissionForRole(@RequestBody RolesPermission rolesPermission) throws Exception {
        if (rolesPermissionService.isExact(rolesPermission)==null){
            //在角色资源表插入该信息
            Permission permission = permissionService.getOneById(rolesPermission.getPermissionId());
            rolesPermissionService.insertRolesPermission(rolesPermission);
//            RoleResource roleResource = new RoleResource();
//            roleResource.setRoleId(rolesPermission.getRoleId());
//            roleResource.setResourceId(permission.getResourceId());
//            if (roleResourceService.doesRoleResourceExist(roleResource)==null){
//                roleResourceService.insertRoleResource(roleResource);
//            }
            //将该资源的父亲资源加入角色-资源表
            //获取该资源的bean
            Resource resource = resourceService.getOneById(permission.getResourceId());
            //获取该资源的父亲bean
            Resource parentResource = resourceService.getOneById(resource.getpId());
            //生成父亲资源关系
            RoleResource parentRoleResource = new RoleResource();
            parentRoleResource.setRoleId(rolesPermission.getRoleId());
            parentRoleResource.setResourceId(resource.getpId());
            //插入父亲资源关系
            if (roleResourceService.doesRoleResourceExist(parentRoleResource) ==null){
                roleResourceService.insertRoleResource(parentRoleResource);
            }
            return DataResult.success();
        }
        else {
            return DataResult.failWithMsg("该角色已经拥有该权限");
        }
    }
    @PostMapping("/deleteOnePermission")
    public DataResult deleteOnePermission(@RequestParam Map<String,String> map) throws Exception {
        //创建要删除的目标
        RolesPermission rolesPermission = rolesPermissionService.getOneById(Integer.valueOf(map.get("srpId")));
        Integer roleId = rolesPermission.getRoleId();
        Integer permissionId = rolesPermission.getPermissionId();
        //查询是否有该用户权限关系
        RolesPermission deletePermission = rolesPermissionService.isExact(rolesPermission);
        Permission toDelPermission = permissionService.getOneById(permissionId);
        if (deletePermission!= null){
            //删除该权限
            rolesPermissionService.deleteOneById(deletePermission.getId());
            //获取这个角色的所有权限
            List<Permission> permissions = rolesPermissionService.getAllByRoleId(roleId);
            //判断删除权限的是否还有二级同类型权限
            if (hasSamePermission(permissions,toDelPermission)){
                //删除角色-资源表的关系resourceId pid
                Resource childResource = resourceService.getOneById(toDelPermission.getResourceId());
                RoleResource parentRoleResource = new RoleResource(roleId,childResource.getpId());
                RoleResource parentRoleResourceD = roleResourceService.doesRoleResourceExist(parentRoleResource);
                roleResourceService.deleteOneById(parentRoleResourceD.getId());
            }
            //完成删除
        }
        return DataResult.success(200,"success","删除权限成功");
    }
    private boolean hasSamePermission(List<Permission> permissions,Permission permission){
        for (Permission permissionEct:permissions) {
            if(permissionEct.getResourceName().equals(permission.getResourceName())){
                return false;
            }
        }
        return true;
    }
}
